package com.xu.cloud.common.mybatis;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.InitializingBean;

import java.util.List;

/**
 * 向SqlSessionFactory中添加MybatisLogInterceptor日志打印拦截器
 *
 * @author xuguan
 * @since 2025/2/11
 */
public class SqlSessionFactoryLogging implements InitializingBean {
    private final List<SqlSessionFactory> sqlSessionFactoryList;

    public SqlSessionFactoryLogging(List<SqlSessionFactory> sqlSessionFactoryList) {
        this.sqlSessionFactoryList = sqlSessionFactoryList;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        addMybatisLogInterceptor();
    }

    public void addMybatisLogInterceptor() {
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(new MybatisLogInterceptor());
        }
    }
}
